import type { ProFormInstance } from '@ant-design/pro-form';
import {
  DrawerForm,
  ProFormDigit,
  ProFormSelect,
  ProFormSwitch,
  ProFormText,
  ProFormTextArea,
} from '@ant-design/pro-form';
import React, { useRef } from 'react';
import { useModel } from 'umi';

import * as c from '@/services/consts';
import { Col, Row } from 'antd';

type FormProps = {
  visible: boolean;
  defaultProductId: number | undefined;
  current: API.ServiceTypeModel | undefined;
  onCancel: () => void;
  onSubmit: (values: API.ServiceTypeModel) => Promise<void>;
};

const Form: React.FC<FormProps> = (props) => {
  const { visible, defaultProductId, current, onCancel, onSubmit } = props;
  if (!visible) {
    return null;
  }

  let defaultCurrent: Partial<API.ServiceTypeModel> | undefined = undefined;
  if (!current) {
    defaultCurrent = {
      sort: 1,
      status: true,
      productId: defaultProductId,
      config: {
        userSpeedLimit: 0,
      },
    };
  }

  const formRef = useRef<ProFormInstance>();

  const { getProductOptions } = useModel('useGlobalModel', (model) => ({
    getProductOptions: model.getProductOptions,
  }));

  const productOptions = getProductOptions(false);

  return (
    <DrawerForm<API.ServiceTypeModel>
      open={visible}
      formRef={formRef}
      autoFocusFirstInput
      width={800}
      title={`${current ? '编辑' : '添加'}服务类型`}
      initialValues={current ? current : defaultCurrent}
      onFinish={onSubmit}
      drawerProps={{
        // forceRender: true,
        // destroyOnClose: true,
        // closable: false,
        onClose: onCancel,
        maskClosable: false,
      }}
    >
      <Row gutter={24}>
        <Col span={24}>
          <ProFormSelect
            name="productId"
            label="产品"
            options={productOptions}
            rules={[{ required: true, message: '请选择产品' }]}
          />
        </Col>
        <Col span={24}>
          <ProFormSelect
            name="memberLevel"
            label="用户等级"
            options={c.MemberLevelOptions}
            rules={[{ required: true, message: '请选择用户等级' }]}
          />
        </Col>
        <Col span={24}>
          <ProFormText
            name="name"
            label="名称"
            rules={[{ required: true, message: '请输入名称' }]}
            placeholder="请输入名称"
          />
        </Col>
        <Col span={24}>
          <ProFormDigit
            name="deviceLimit"
            label="允许设备数量"
            min={0}
            max={1024}
            addonAfter={
              <span className="ant-form-item-extra">
                (空或者0表示使用系统配置)
              </span>
            }
          />
        </Col>
        <Col span={24}>
          <ProFormDigit
            name={['config', 'userSpeedLimit']}
            label="用户限速"
            min={0}
            max={1024000}
            addonAfter={
              <span className="ant-form-item-extra">(KB, 0为不限速)</span>
            }
          />
        </Col>
        <Col span={24}>
          <ProFormDigit name="sort" label="排序" min={0} max={9999} />
        </Col>
        <Col span={24}>
          <ProFormSwitch name="status" label="状态" />
        </Col>
        <Col span={24}>
          <ProFormTextArea name="remark" label="备注" />
        </Col>
      </Row>
    </DrawerForm>
  );
};

export default Form;
